package ca.polymtl.rubikcube.CubeActivity.model;

import java.util.HashMap;

import ca.polymtl.rubikcube.CubeActivity.model.Rubik.RotationDirection;
import ca.polymtl.rubikcube.util.Color;
import ca.polymtl.rubikcube.util.Position;

/**
 * Represents one of the 27 cubes of a Rubik's cube. The sides of a Cubelet
 * matches the sides of the {@link Rubik} it belongs to (FRONT of the Cubelet is
 * aligned with FRONT of the Rubik, etc).
 * 
 * @author markys7
 * 
 */
public class Cubelet {
	/**
	 * Maps a side of the {@link Cubelet} with a {@link Color}.
	 */
	private HashMap<Side, Color> sides = new HashMap<Side, Color>();

	private Side selectedSide = null;
	
	public Position pos;
	
	public void selectSide(Side side) {
		selectedSide = side;
	}
	
	public void unselect() {
		selectedSide = null;
	}
	
	public Side getSelectedSide() {
		return selectedSide;
	}

	/**
	 * Gets the color of a side.
	 * 
	 * @param side
	 *            The side.
	 * @return The color if a color has been set previously for this side, null
	 *         otherwise.
	 */
	public Color getSideColor(Side side) {
		return sides.get(side);
	}

	/**
	 * Sets the color of a side.
	 * 
	 * @param side
	 *            One of {@link Side}.
	 * @param color
	 *            One of {@link Color}.
	 */
	public void setSideColor(Side side, Color color) {
		sides.put(side, color);
	}

	/**
	 * Sets the color of all the sides.
	 * 
	 * @param color
	 *            The color.
	 */
	public void setAllSidesColor(Color color) {
		for (Side side : Side.values()) {
			sides.put(side, color);
		}
	}

	/**
	 * Clears the information about the color of a side.
	 * 
	 * @param side
	 */
	public void clearSideColor(Side side) {
		sides.remove(side);
	}

	/**
	 * Apply a rotation to the colors of the cubelet
	 * 
	 * @param side 
	 * @param way
	 */
	public void rotate(Side side, RotationDirection way) {
		CubeletTransitions ct = CubeletTransitions.getTransitions(side, way);
		Side[] s = ct.sides;

		Color c0 = sides.get(s[0]);
		Color c1 = sides.get(s[1]);
		Color c2 = sides.get(s[2]);
		Color c3 = sides.get(s[3]);

		sides.put(s[1], c0);
		sides.put(s[2], c1);
		sides.put(s[3], c2);
		sides.put(s[0], c3);
	}
}
